package spe.model.registry;

import java.util.HashMap;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;

import spe.common.Logger;

public class SPERegistryRegistry 
{
	private static HashMap<String, String> editorRegistry = new HashMap<String, String>();
	private static HashMap<String, SPEElementRegistry> semanticElementRegistries = new HashMap<String, SPEElementRegistry>();
	private static HashMap<String, SPEXmlAdapterRegistry> xmlAdapterRegistries = new HashMap<String, SPEXmlAdapterRegistry>();
	
	static {
		//extension point will be changed to spe.model.dsl
		IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint("spe.model.dsl");
		//IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint("spe.dsl");
		//System.out.println(String.format("[SPERegistryRegistry] extensionPoint: %s", extensionPoint));
		IExtension[] extensions = extensionPoint.getExtensions();
		for (int i = 0; i < extensions.length; i++) 
		{
			IConfigurationElement[] configElements = extensions[i].getConfigurationElements();
			for (int j = 0; j < configElements.length; j++) 
			{
				String editorId = configElements[j].getAttribute("editor");
				String language = configElements[j].getAttribute("id");
				//System.out.println(String.format("[SPERegistryRegistry] EditorId: %s; Language: %s", editorId, language));
				editorRegistry.put(editorId, language);
			}
		}				
	}
		
	public static SPEElementRegistry getSPEElementRegistry(String editorId) throws RuntimeException 
	{
		SPEElementRegistry result = (SPEElementRegistry)semanticElementRegistries.get(editorId);
		if (result == null) {
			String language = (String)editorRegistry.get(editorId);
			if (language != null) {
				result = new SPEElementRegistry(language);
			}
		}
		if (result == null) {
			handleError(editorId);
		}
		return result;
	}

	private static void handleError(String editorId) 
	{
		String message = "Editor " + editorId + " has no associated language";
		RuntimeException e = new RuntimeException(message);
		Logger.logError(message, e);
		throw e;
	}
	
	public static SPEXmlAdapterRegistry getXmlAdapterRegistry(String editorId) throws RuntimeException 
	{
		SPEXmlAdapterRegistry result = (SPEXmlAdapterRegistry)xmlAdapterRegistries.get(editorId);
		if (result == null) {
			String language = (String)editorRegistry.get(editorId);
			if (language != null) 
			{
				SPEElementRegistry semanticElementRegistry = getSPEElementRegistry(editorId);
				if (semanticElementRegistry != null) 
				{
					result = new SPEXmlAdapterRegistry(semanticElementRegistry.getSemanticElementIds());
					//System.out.println(String.format("[SPERegistryRegistry] EditorId: %s; result: %s", editorId, result));
				}
			}
		}
		
		if (result == null) 
			handleError(editorId);		
		return result;
	}

}
